//获取用户所在城市信息
function showCityInfo(getOne) {
    getOne = getOne | 0;
    var map, citysearch;
    //加载地图，调用浏览器定位服务
    map = new AMap.Map('container1');
    var resultContent;
    //实例化城市查询类
    map.plugin('AMap.CitySearch', function() {
        citysearch = new AMap.CitySearch();
        //自动获取用户IP，返回当前城市
        citysearch.getLocalCity(function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                if (result && result.city && result.bounds) {
                    var scity = result.city.substr(0, (result.city.length - 1));
                    if (getOne) {
                        document.querySelector('#city a').innerHTML = scity;

                        // resultContent = onCityOne(result.city);
                    } else {
                        resultContent = onCityComplete(scity);
                    }
                    console.log(resultContent);
                    // return resultContent;
                }
            }
        });
    });
}

// 成功回调获取区列表
function onCityComplete(data) {
    var result;
    var cityinfo = data;
    var url = encodeURI('http://www.xiaiyepu.cn/mobile/client_api.php?act=find_district&city=' + cityinfo);
    mui.post(url, function(data) {
        //获得服务器响应
        result = {
            success: success,
            message: data.m,
            content: data.o
        }
        return result;
    }, 'json');
}


// 仅获取城市位置
function onCityOne(data) {
    var result = {
        success: 1,
        message: '',
        content: data
    };
    return result;
}

// 获取经纬度坐标
function msGetPosition() {
    //    

    var map, geolocation;
    //加载地图，调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable: true
    });
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true, //是否使用高精度定位，默认:true
            timeout: 10000, //超过10秒后停止定位，默认：无穷大
            buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition: 'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
        AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
    });
}

//解析定位结果
function onComplete(data) {
    var result;
    var str = ['定位成功'];
    str.push('经度：' + data.position.getLng());
    str.push('纬度：' + data.position.getLat());
    if (data.accuracy) {
        str.push('精度：' + data.accuracy + ' 米');
    } //如为IP精确定位结果则没有精度信息
    str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
    console.log(str.join('<br>'));
    // document.getElementById('tip').innerHTML = str.join('<br>');
    var url = 'http://www.xiaiyepu.cn/mobile/client_api.php?act=update_location';
    mui.post(url, {
        user_location: data.position.getLng() + ',' + data.position.getLat()
    }, function(data) {
        //服务器返回响应，根据响应结果，分析是否登录成功；
        result = {
            success: data.c,
            message: data.m
        };
        return result;
    }, 'json');
}

//定位失败
function onError(data) {
    var message = '';
    var result = { success: 0, message: message };
    return result;
}